﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBCM;

namespace WindowsForms.PBCM.usercontrol
{
    public partial class UCChiTietThietBi : UserControl
    {

        ChiTietThietBi da;
        public UCChiTietThietBi()
        {
            InitializeComponent();
            dataGridViewChiTietThietBi.AutoGenerateColumns = false;
            //LoadChiTietThietBi(ChiTietThietBi.Objects.All().ToList());
            LoadChiTietThietBi(ChiTietThietBi.Objects.All().ToList());
            //ChiTietThietBi.Objects.All().ToList();
        }

        private void LoadChiTietThietBi(List<ChiTietThietBi> list)
        {
            dataGridViewChiTietThietBi.DataSource = list;

        }

        private void dataGridViewChiTietThietBi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                da = ChiTietThietBi.Objects.GetById(dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[0].Value.ToString());

                if (e.ColumnIndex == 6)
                {
                    ChiTietThietBi a = new ChiTietThietBi();
                    a.MaChiTietThietBi = dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[0].Value.ToString();
                    a.MaThietBi = dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[1].Value.ToString();
                    a.SoLuong = (int)dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[2].Value;
                    a.TinhTrang = (int)dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[3].Value;
                    a.NgayCapNhat = (DateTime)dataGridViewChiTietThietBi.Rows[e.RowIndex].Cells[4].Value;

                    try
                    {
                        if (ChiTietThietBi.Objects.Update(a))
                        {
                            MessageBox.Show("Sửa thành công");
                            LoadChiTietThietBi(ChiTietThietBi.Objects.All().ToList());
                        }
                    }

                    catch (Exception)
                    {
                        LoadChiTietThietBi(ChiTietThietBi.Objects.All().ToList());
                        throw;

                    }
                }
                if (e.ColumnIndex == 5)
                {
                    //ltd = LichThiDau.Objects.GetById(dgvLTD.Rows[e.RowIndex].Cells[0].Value.ToString());
                    try
                    {
                        if (ChiTietThietBi.Objects.Delete(da))
                        {
                            MessageBox.Show("Xóa thành công");
                            LoadChiTietThietBi(ChiTietThietBi.Objects.All().ToList());
                        }
                    }

                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
        }

        private void buttonXTim_Click(object sender, EventArgs e)
        {
            try
            {
                if (radioButtonMa.Checked == true)
                {
                    List<ChiTietThietBi> tam = new List<ChiTietThietBi>();
                    tam.Add(ChiTietThietBi.Objects.GetById(textBoxXTim.Text));
                    LoadChiTietThietBi(tam);
                }
                if (radioButtonMaThietBi.Checked == true)
                {
                    LoadChiTietThietBi(ChiTietThietBi.Objects.Find(x => x.MaThietBi == textBoxXTim.Text).ToList());
                }
                if (radioButtonTinhTrang.Checked == true)
                {
                    LoadChiTietThietBi(ChiTietThietBi.Objects.Find(x => x.TinhTrang == Convert.ToInt32(textBoxXTim.Text)).ToList());
                }
            }
            catch (Exception)
            {

            }
        }
        private void textBoxXTim_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (radioButtonTinhTrang.Checked == true)
            {
                if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                    e.Handled = true;
            }
        }

        private void radioButtonMa_CheckedChanged(object sender, EventArgs e)
        {
            textBoxXTim.ResetText();
        }

        private void radioButtonMaThietBi_CheckedChanged(object sender, EventArgs e)
        {
            textBoxXTim.ResetText();
        }

        private void radioButtonTinhTrang_CheckedChanged(object sender, EventArgs e)
        {
            textBoxXTim.ResetText();
        }
    }
}
